package com.anywhere.freightman.system.controller;

import com.anywhere.freightman.car.model.DriverModel;
import com.anywhere.freightman.car.service.IDriverService;
import com.anywhere.freightman.common.ContextKeys;
import com.anywhere.freightman.common.annotation.Permission;
import com.anywhere.freightman.common.entity.ReturnResp;
import com.anywhere.freightman.common.entity.user.Account;
import com.anywhere.freightman.common.enumeration.RoleType;
import com.anywhere.freightman.system.controller.resp.KeyValueResp;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("data")
public class DataController {

    @Autowired
    private IDriverService driverService;

    @GetMapping("driver")
    @Permission(code = "COMMON")
    @ApiOperation("获取司机列表")
    public ReturnResp getDriverList(HttpServletRequest httpRequest) {
        Account account = (Account) httpRequest.getAttribute(ContextKeys.REQUEST_ACCOUNT);
        List<KeyValueResp> list = new ArrayList<>();
        List<DriverModel> driverList;
        if (account.getType().equals(RoleType.ADMIN)) {
            driverList = driverService.getAll();
        } else {
            driverList = driverService.getListByCompanyId(account.getCompanyId());
        }

        driverList.forEach(item -> {
            KeyValueResp resp = new KeyValueResp();
            resp.setText(item.getRealName());
            resp.setValue(item.getId());
            list.add(resp);
        });

        return ReturnResp.buildOk(list);
    }

}
